﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MySql.Data.MySqlClient;

public class TaskFactory
{
    private static TaskFactory instance;
    private MysqlConnector connector;

    private TaskFactory()
    {
        connector = MysqlConnector.getInstance();
	}

    public static TaskFactory getInstance()
    {
        if (instance == null)
        {
            instance = new TaskFactory();
        }
        return instance;
    }

    public List<Task> getTasksForProject(int projectId)
    {
        String query = "SELECT * FROM task WHERE project = " + projectId;
        MySqlDataReader dr = connector.Select(query);
        List<Task> tasks = new List<Task>();

        while (dr.Read())
        {
            Task newTask = new Task();
            newTask.name = dr.GetString(1);
            newTask.description = dr.GetString(2);
            newTask.created = dr.GetDateTime(3);
            newTask.finished = dr.GetDateTime(4);
            newTask.timeSpent = dr.GetDouble(5);
            newTask.status = (Status)Enum.Parse(typeof(Status), dr.GetString(6));
            newTask.priority = (Priority)Enum.Parse(typeof(Priority), dr.GetString(7));
            newTask.createdBy = UserFactory.getInstance().getUserById(dr.GetInt32(8));
            newTask.assigned = UserFactory.getInstance().getUserById(dr.GetInt32(9));

            tasks.Add(newTask);
        }
        return tasks;
    }
    
}
